package auth

import (
	"errors"
	"strings"
	"encoding/gob"
)

/**
@attr status int -1 未登录; 0 已经登录
@attr info map[string]string  session["userInfo"]内容
@attr isLoggedIn bool
 */
type User struct  {
	Info Info
	IsLoggedIn bool
}

type Info struct {
	Username string
	Mail string
	Fullname string
	AuthSessionId string
	GroupPaths GroupRoles
}

//注册User结构,session中使用
func init() {
	gob.Register(&User{})
}


func (u *User) resetUserInfo()  {
	u.Info = Info{}
	u.IsLoggedIn = false
}

func (u *User) info() Info {
	return u.Info
}

func (u *User) name() string {
	return u.Info.Username
}

//func (u *User) LoginOut()  {
//	//session 注销
//	u.resetUserInfo() //单例设置为初始值
//}

func (u *User) login() (*User, error)  {
	var msg []string
	if u.Info.Username == "" {
		msg = append(msg, "Username empty")
	}else if u.Info.Fullname == "" {
		msg = append(msg, "Fullname empty")
	}else {
		u.IsLoggedIn = true
		return u, nil
	}
	return nil, errors.New(strings.Join(msg,","))
}
